﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BBMS.DLL.Base;
using BBMS.DLL.DataContract;

namespace BBMS.DLL.Forms
{
    public partial class frmImportDiscount2 : Form
    {
        NhaCungCap curNcc = null;
        long giaTriChietKhau = 0;
        long importID;
        DiscountList discountList;
        public frmImportDiscount2(NhaCungCap ncc, long _importID, 
            long giaTriCK, 
            DiscountList _discountList)
        {
            InitializeComponent();
            curNcc = ncc;
            this.giaTriChietKhau = giaTriCK;
            this.importID = _importID;
            discountList = _discountList;
        }

        private void frmImportDiscount_Load(object sender, EventArgs e)
        {
            txtMaNCC.Text = curNcc.Ma;
            txtMST.Text = curNcc.MSTVAT;
            txtTenNCC.Text = curNcc.Ten;
            txtDC.Text = curNcc.DiaChi;
            txtChietKhau.Value = giaTriChietKhau;
            if(discountList != null){
                foreach(Discount obj in discountList.AccountedValues){
                    int index = dgvCThu.Rows.Add();
                    dgvCThu.Rows[index].Cells["ID"].Value = obj.ID;
                    dgvCThu.Rows[index].Cells["Chon"].Value = obj.Chon;
                    dgvCThu.Rows[index].Cells["NgayCT"].Value = obj.NgayCT.ToString("dd/MM/yyyy");
                    //dgvCThu.Rows[index].Cells["MaCT"].Value = obj.MaCT;
                    //dgvCThu.Rows[index].Cells["SoCT"].Value = obj.SoCT;
                    //dgvCThu.Rows[index].Cells["LoaiCT"].Value = obj.LoaiCT;
                    dgvCThu.Rows[index].Cells["PhaiThu"].Value = obj.GiaTriPhaiThu;
                    dgvCThu.Rows[index].Cells["ConPhaiThu"].Value = obj.GiaTriPhaiThu - obj.GiaTriDaHoachToan;
                    dgvCThu.Rows[index].Cells["HoachToan"].Value = obj.GiaTriHoachToan;
                    dgvCThu.Rows[index].Tag = obj;
                }
            }
        }

        private void dtDenNgay_ValueChanged(object sender, EventArgs e)
        {
            if (dtDenNgay.Value.Date < dtTuNgay.Value.Date)
                dtDenNgay.Value = dtTuNgay.Value.Date;
        }

        private void dtTuNgay_ValueChanged(object sender, EventArgs e)
        {
            if (dtDenNgay.Value.Date < dtTuNgay.Value.Date)
                dtDenNgay.Value = dtTuNgay.Value.Date;
        }

        private void btnLoc_Click(object sender, EventArgs e)
        {
            /*if(cbxLoaiCTThu.SelectedIndex < 0){
                MessageBox.Show("Hãy chọn loại chứng từ thu");
                return;
            }*/

            FilterParams fParams = new FilterParams();
            fParams.ArParams.Add(new FilterCondition("NgayCT","TuNgay",dtTuNgay.Value.Date,SQLOperator.GREATER_OR_EQUAL));
            fParams.ArParams.Add(new FilterCondition("NgayCT","DenNgay",dtDenNgay.Value.Date,SQLOperator.LOWER_OR_EQUAL));
            //string strExWhere = "";
            /*if(cbxLoaiCTThu.SelectedIndex == 0)
                strExWhere = " (LoaiCT = 'CTCK' OR LoaiCT = 'CKTM') ";
            else if(cbxLoaiCTThu.SelectedIndex == 1) 
                strExWhere = " (LoaiCT = 'CTCK') ";
            else if (cbxLoaiCTThu.SelectedIndex == 2) 
                strExWhere = " (LoaiCT = 'CKTM') ";
            */
            //strExWhere = " (LoaiCT = 'CTCK') ";
            discountList.LoadAvailableDiscount(fParams);
            //Hashtable hash = BBMS.DLL.DataContract.ImportDiscountHelper.GetDiscountByImport(importID,  fParams, strExWhere);
            dgvCThu.Rows.Clear();
            
            foreach (Discount obj in discountList.AccountedValues)
            {
                int index = dgvCThu.Rows.Add();
                dgvCThu.Rows[index].Cells["ID"].Value = obj.ID;
                dgvCThu.Rows[index].Cells["Chon"].Value = obj.Chon;
                dgvCThu.Rows[index].Cells["NgayCT"].Value = obj.NgayCT.ToString("dd/MM/yyyy");
                /*dgvCThu.Rows[index].Cells["MaCT"].Value = obj.MaCT;
                dgvCThu.Rows[index].Cells["SoCT"].Value = obj.SoCT;
                dgvCThu.Rows[index].Cells["LoaiCT"].Value = obj.LoaiCT;*/
                dgvCThu.Rows[index].Cells["PhaiThu"].Value = obj.GiaTriPhaiThu;
                dgvCThu.Rows[index].Cells["ConPhaiThu"].Value = obj.GiaTriPhaiThu - obj.GiaTriDaHoachToan;
                dgvCThu.Rows[index].Cells["HoachToan"].Value = obj.GiaTriHoachToan;
                dgvCThu.Rows[index].Tag = obj;
            }

            foreach (Discount obj in discountList.AvaiableValues)
            {
                int index = dgvCThu.Rows.Add();
                dgvCThu.Rows[index].Cells["ID"].Value = obj.ID;
                dgvCThu.Rows[index].Cells["Chon"].Value = obj.Chon;
                dgvCThu.Rows[index].Cells["NgayCT"].Value = obj.NgayCT.ToString("dd/MM/yyyy");
                /*dgvCThu.Rows[index].Cells["MaCT"].Value = obj.MaCT;
                dgvCThu.Rows[index].Cells["SoCT"].Value = obj.SoCT;
                dgvCThu.Rows[index].Cells["LoaiCT"].Value = obj.LoaiCT;*/
                dgvCThu.Rows[index].Cells["PhaiThu"].Value = obj.GiaTriPhaiThu;
                dgvCThu.Rows[index].Cells["ConPhaiThu"].Value = obj.GiaTriPhaiThu - obj.GiaTriDaHoachToan;
                dgvCThu.Rows[index].Cells["HoachToan"].Value = obj.GiaTriHoachToan;
                dgvCThu.Rows[index].Tag = obj;
            }


        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Escape:
                    this.DialogResult = DialogResult.Cancel;
                    return true;
                case Keys.Enter:
                    if (!(this.ActiveControl is Button))
                    {
                        SendKeys.Send("{TAB}");
                        return true;
                    }
                    break;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
        //public ChungTuThu RetChungTuThu = null;
        public DiscountList DiscountList
        {
            get{
                return this.discountList;
            }
        }
        public long ChietKhau
        {
            get { return txtChietKhau.Value; }
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            long totalKhauTru = 0;
            for(int i = 0 ; i < dgvCThu.Rows.Count ; i ++){
                DataGridViewCellCollection cels = dgvCThu.Rows[i].Cells;
                if ((bool)cels["Chon"].Value == true)
                {
                    totalKhauTru += CommonTools.Helper.StringToLong(cels["HoachToan"].Value.ToString());
                }
            }
            if (totalKhauTru > txtChietKhau.Value)
            {
                MessageBox.Show(string.Format("Tổng giá trị hoạch toán ({0}) không thể lớn hơn giá trị khấu trừ ({1}).", 
                    CommonTools.Helper.MoneyString(totalKhauTru),
                    CommonTools.Helper.MoneyString(txtChietKhau.Value)));
                return;
            }
            for (int i = 0; i < dgvCThu.Rows.Count; i++)
            {
                DataGridViewCellCollection cels = dgvCThu.Rows[i].Cells;
                if ((bool)cels["Chon"].Value == true)
                {
                    discountList.Add(new Discount((long)cels["ID"].Value,
                        //cels["MaCT"].ToString(),
                        //cels["SoCT"].Value.ToString(),
                        DateTime.Today.Date,
                        //cels["LoaiCT"].Value.ToString(),
                        CommonTools.Helper.StringToLong(cels["PhaiThu"].Value.ToString()),
                        CommonTools.Helper.StringToLong(cels["ConPhaiThu"].Value.ToString()),
                        0,
                        CommonTools.Helper.StringToLong(cels["HoachToan"].Value.ToString()),
                        true,
                        TrangThaiThanhToan.ChuaTT));
                }
            }
            this.DialogResult = DialogResult.OK;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
        }

        private long TongHoachToan
        {
            get
            {
                long retVal = 0;
                for (int i = 0; i < dgvCThu.Rows.Count; i++) {
                    if ((bool)dgvCThu.Rows[i].Cells["Chon"].Value == true)
                    {
                        retVal += CommonTools.Helper.StringToLong(dgvCThu.Rows[i].Cells["HoachToan"].Value.ToString());
                    }
                }
                return retVal;
            }
        }

        private long SoDuConChietKhau
        {
            get {
                return txtChietKhau.Value - TongHoachToan;
            }
        }

        private void dgvCThu_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 5)// HoachToan
                {
                    txtHoachToan.Value = TongHoachToan;
                    if (dgvCThu.Rows[e.RowIndex].Tag != null)
                    {
                        Discount obj = (Discount)dgvCThu.Rows[e.RowIndex].Tag;
                        dgvCThu.Rows[e.RowIndex].Cells["ConPhaiThu"].Value = 
                            obj.GiaTriPhaiThu - obj.GiaTriDaHoachToan + obj.GiaTriHoachToan -
                            CommonTools.Helper.StringToLong(dgvCThu.Rows[e.RowIndex].Cells["HoachToan"].Value.ToString());
                    }
                }
                else if (e.ColumnIndex == 1) // Chon
                {
                    if ((bool)dgvCThu.Rows[e.RowIndex].Cells[1].Value == false)
                    {
                        dgvCThu.Rows[e.RowIndex].Cells["HoachToan"].Value = 0;
                    }
                    else
                    {
                        long min = this.SoDuConChietKhau;
                        long conPhaiTT = CommonTools.Helper.StringToLong(dgvCThu.Rows[e.RowIndex].Cells["ConPhaiThu"].Value.ToString());
                        if (min > conPhaiTT) dgvCThu.Rows[e.RowIndex].Cells["HoachToan"].Value = conPhaiTT;
                        else dgvCThu.Rows[e.RowIndex].Cells["HoachToan"].Value = min;
                    }
                }
            }
            catch
            {
            }
        }

        private void dgvCThu_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dgvCThu.IsCurrentCellDirty)
            {
                dgvCThu.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
            /*
             if (e.ColumnIndex == 1) // Chon
            {
                if ((bool)dgvCThu.Rows[e.RowIndex].Cells[1].Value == true)
                {
                    dgvCThu.Rows[e.RowIndex].Cells["HoachToan"].Value = 0;
                }
                else
                {
                    long min = this.SoDuConChietKhau;
                    long conPhaiTT = CommonTools.Helper.StringToLong(dgvCThu.Rows[e.RowIndex].Cells["ConPhaiThu"].Value.ToString());
                    if (min > conPhaiTT) dgvCThu.Rows[e.RowIndex].Cells["HoachToan"].Value = conPhaiTT;
                    else dgvCThu.Rows[e.RowIndex].Cells["HoachToan"].Value = min;
                }
            }
            */
        }
        
    }
}
